package com.xingkeduo.business.operation.domain;

import com.xingkeduo.business.operation.service.CitiesService;
import com.xingkeduo.business.operation.service.OfficeService;
import com.xingkeduo.business.operation.service.ShopsService;
import com.xingkeduo.pojo.dto.OfficeDTO.OfficeDTOS;
import com.xingkeduo.pojo.query.OfficeQuery;
import com.xingkeduo.pojo.query.ShopsQuery;
import com.xingkeduo.pojo.table.CitiesTable;
import com.xingkeduo.pojo.table.CountryTable;
import com.xingkeduo.pojo.table.OfficeTable;
import com.xingkeduo.pojo.table.ShopsTable;
import com.xingkeduo.util.pojo.domain.Page;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 商户domain
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Component
public class OfficeDomain {

    @Autowired
    private OfficeService officeService;

    @Autowired
    private CitiesService citiesService;

    @Autowired
    private ShopsService shopsService;

    public OfficeTable getOfficeInfoById(Long id) throws Throwable {
        OfficeTable officeTable = officeService.selectById(id);

        return officeTable;
    }

    public List<OfficeTable> getOfficeList(OfficeQuery officeQuery) throws Throwable {
        List<OfficeTable> officeList = officeService.selectLists(officeQuery);

        return officeList;
    }

    //分页查询
    public Page<OfficeDTOS> getOfficePageList(OfficeQuery officeQuery) throws Throwable {
        Integer pageNo = officeQuery.getPageNo();
        Integer pageSize = officeQuery.getPageSize();
        Page<OfficeTable> officeTablePage = officeService.selectPageLists(officeQuery,pageNo,pageSize);

        List<OfficeTable> dtoList = officeTablePage.getDtoList();

        ArrayList<OfficeDTOS> objects = new ArrayList<>();
        if(!CommonUtil.isEmpty(dtoList)){
            for (OfficeTable officeTable : dtoList) {
                OfficeDTOS vendorDTO = new OfficeDTOS();
                BeanUtils.copyProperties(officeTable,vendorDTO);
                int shopsNum = shopsService.count(new ShopsQuery().setOfficeId(officeTable.getId()).setStatus(2));
                vendorDTO.setShopsNum(shopsNum);
                objects.add(vendorDTO);
            }
        }
        Page<OfficeDTOS> officeDTOPage = new Page<>(pageNo,pageSize);
        BeanUtils.copyProperties(officeTablePage, officeDTOPage);
        officeDTOPage.setDtoList(objects);
        return officeDTOPage;
    }

    public int createOffice(OfficeTable officeTable) throws Throwable {

        if(!CommonUtil.isEmpty(officeTable.getName())){
            List<OfficeTable> officeTableList = officeService.selectLists(new OfficeQuery().setNameRepeat(officeTable.getName()));
            if(!CommonUtil.isEmpty(officeTableList)){
                throw new DataException(DataException.Type.DEFAULT, "分公司名字已经存在");
            }
        }


        CitiesTable citiesTable = new CitiesTable();
        if(!CommonUtil.isEmpty(officeTable.getCitiesId())){
            citiesTable = citiesService.selectById(officeTable.getCitiesId());
        }
        if(!CommonUtil.isEmpty(citiesTable)){
            officeTable.setCitiesName(citiesTable.getName());
            officeTable.setVendorId(citiesTable.getVendorId());
            officeTable.setVendorName(citiesTable.getVendorName());
        }

        return officeService.insert(officeTable);
    }

    public int modifyOffice(OfficeTable officeTable) throws Throwable {
        OfficeTable officeTable1 = officeService.selectById(officeTable.getId());

        if(!CommonUtil.isEmpty(officeTable.getName())){
            if(!officeTable.getName().equals(officeTable1.getName())){
                if(!CommonUtil.isEmpty(officeTable.getName())){
                    List<OfficeTable> officeTableList = officeService.selectLists(new OfficeQuery().setNameRepeat(officeTable.getName()));
                    if(!CommonUtil.isEmpty(officeTableList)){
                        throw new DataException(DataException.Type.DEFAULT, "分公司名字已存在");
                    }
                }
            }
        }

        //下分公司是否有状态为正常
        if(!CommonUtil.isEmpty(officeTable.getStatus())){

            if(officeTable.getStatus() ==2){
                List<ShopsTable> shopsTableList= shopsService.selectLists(new ShopsQuery().setOfficeId(officeTable.getId()).setIsStatus(Long.valueOf(3)));
                if(!CommonUtil.isEmpty(shopsTableList) && shopsTableList.size() > 0){
                    throw new DataException(DataException.Type.DEFAULT, "该分公司下，还存在有效的门店，无法停用分公司!");
                }
            }else{


                if(!CommonUtil.isEmpty(officeTable1)){
                    CitiesTable citiesTable = citiesService.selectById(officeTable1.getCitiesId());
                    if(citiesTable.getStatus() == 2){
                        throw new DataException(DataException.Type.DEFAULT, "该分公司所对应的城市处于禁用状态，请先启用对应城市!");
                    }
                }
            }
        }


        CitiesTable citiesTable = new CitiesTable();
        if(!CommonUtil.isEmpty(officeTable.getCitiesId())){
            citiesTable = citiesService.selectById(officeTable.getCitiesId());
        }
        if(!CommonUtil.isEmpty(citiesTable)){
            officeTable.setCitiesName(citiesTable.getName());
            officeTable.setVendorId(citiesTable.getVendorId());
            officeTable.setVendorName(citiesTable.getVendorName());
        }
        return officeService.update(officeTable);
    }

    //删除
    public int delOffice(Long id) throws Throwable {

        //判断城市下分公司是否有状态为正常
        if(!CommonUtil.isEmpty(id)){
            List<ShopsTable> shopsTableList= shopsService.selectLists(new ShopsQuery().setOfficeId(id).setIsStatus(Long.valueOf(3)));
            if(!CommonUtil.isEmpty(shopsTableList) && shopsTableList.size() > 0){
                throw new DataException(DataException.Type.DEFAULT, "该分公司下，还存在有效的门店，无法删除分公司!");
            }
        }
        OfficeTable officeTable = new OfficeTable();
        officeTable.setId(id);
        officeTable.setIsDelete(YesNoEnum.YES);
        int delRes = officeService.delete(officeTable);
        if(delRes > 0){
            return delRes;
        }else{
            throw new DataException(DataException.Type.FAIL, "删除失败");
        }
    }

}
